<?php


namespace app\admin\controller;


use app\common\exception\MessageException;
use app\common\model\mysql\User as UserModel;
use app\common\validate\UserValidate;

class Member extends BaseController
{
    public function list()
    {
        $pageSize = $this->request->param('pageSize');
        // 分页查询
        $User = new UserModel();
        $list = $User->paginate($pageSize);
        $list->hidden(['password'])->toArray();
        return $this->renderSuccess(compact('list'));
    }

    public function create()
    {
        if (!$this->request->isPost()) {
            throw new MessageException(['msg' => '请求方法不合法']);
        }
        (new UserValidate())->goCheck();
        $data = $this->request->param();
        try {
            $User = new UserModel();
            $findUser = $User->checkUserByEmail($data['email']);

            if ($findUser) {
                dump($findUser);
                throw new MessageException(['msg' => '用户已存在']);
            }

            $data['password'] = md5($data['password'] . config('setting.pwd_salt'));
            $data['status'] = 0;

            $admin = $User->create($data);
            if ($admin) {
                return $this->renderSuccess(['user' => $admin], '添加成功');
            }

        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    public function update()
    {
        if (!$this->request->isPost()) {
            throw new MessageException(['msg' => '请求方法不合法']);
        }
        return $this->renderSuccess([],'添加成功');
    }

    public function delete()
    {

    }
}